package com.gexiaobao.pigeon.viewmodel

import android.view.View
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.gexiaobao.pigeon.app.model.bean.MoreRaceListResponse
import com.gexiaobao.pigeon.app.model.bean.MyPassResultResponse
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

/**
 * @author:hxw
 * @date  :2023/1/17
 * @desc  :
 */
class MyMatchViewModel : BaseViewModel() {

    val raceTitle = StringObservableField()
    val homingPigeonNum = StringObservableField()
    val isShowNoData = BooleanObservableField()
    val isShowNoList = BooleanObservableField()

    /** 更多比赛列表 */
    var moreRaceResult: MutableLiveData<ListDataUiState<MoreRaceListResponse.DataList>> = MutableLiveData()

    /** 我当前赛事成绩 */
    var myPassResult: MutableLiveData<MyPassResultResponse> = MutableLiveData()

    /** 从更多比赛获取比赛结果 */
    var racePassResult: MutableLiveData<MyPassResultResponse> = MutableLiveData()

    /** 上一关 下一关比赛结果 */
    var toPassResult: MutableLiveData<MyPassResultResponse> = MutableLiveData()


    var noDataVisible = object : ObservableInt(isShowNoData) {
        override fun get(): Int {
            return if (isShowNoData.get()) {
                View.GONE
            } else {
                View.VISIBLE
            }
        }
    }

    var listVisible = object : ObservableInt(isShowNoList) {
        override fun get(): Int {
            return if (isShowNoList.get()) {
                View.VISIBLE
            } else {
                View.GONE
            }
        }
    }

    /** 获取更多比赛列表 */
    fun getMoreRaceList() {
        request({ apiService.getMoreRaceList() }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isRefresh = true, isEmpty = it.isEmpty(), hasMore = false, isFirstEmpty = it.isEmpty(), listData = it.list
            )
            moreRaceResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, errMessage = it.msg, isRefresh = true, listData = arrayListOf<MoreRaceListResponse.DataList>()
            )
            moreRaceResult.value = listDataUiState
        })
    }

    /** 获取我当前赛事成绩 */
    fun getMyPassResult(view: SwipeRefreshLayout) {
        request({ apiService.getMyPassResult() }, {
            myPassResult.value = it
        }, {
            view.isRefreshing = false
            RxToast.showToast(it.msg)
        })
    }

    /** 从更多点击获取赛事结果  */
    fun getRacePassResult(raceId: String) {
        request({ apiService.getRacePassResult(raceId) }, {
            racePassResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 上一关、下一关获取指定关卡成绩  */
    fun getToPassResult(id: String) {
        request({ apiService.getToPassResult(id) }, {
            toPassResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

}